package com.soft.mainsite.module.publishlog.service;

import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.soft.mainsite.common.domain.ErrorCode;
import com.soft.mainsite.common.utils.JsonUtil;
import com.soft.mainsite.module.publishlog.dao.PublishLogDao;
import com.soft.mainsite.module.publishlog.entity.PublishLog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * PublishLogServiceImpl.java 发布日志服务层接口的实现类……
 *
 * @author hujm
 * @date 2019年04月26日 15:58
 */
@Slf4j
@Service
public class PublishLogServiceImpl implements PublishLogService {

    @Autowired
    private PublishLogDao publishLogDao;

    /**
     * 保存日志
     *
     * @param publishLog 日志实体
     * @return 影响行数
     * @author 胡佳明
     * @date 2019/4/26 15:57
     */
    @Override
    public String save(PublishLog publishLog) {
        publishLog.setCreateTime(new Date());
        int i = publishLogDao.save(publishLog);
        if (i > 0){
            return JsonUtil.toSuccess("保存日志成功!", "publishLog-tab", false);
        }
        return JsonUtil.toError("保存日志失败!");
    }

    /**
     * 保存日志
     *
     * @param siteId 站点ID
     * @param categoryId 栏目ID
     * @param tplName 模板名称
     * @param errorCode 错误码
     * @return
     * @author 胡佳明
     * @date 2019/4/26 15:57
     */
    @Override
    public String save(Integer siteId, Long categoryId, String tplName, int errorCode) {
        PublishLog publishLog = new PublishLog();
        publishLog.setSiteId(siteId);
        publishLog.setCategoryId(Convert.toInt(categoryId));
        publishLog.setTemplateName(tplName);
        publishLog.setDescription(ErrorCode.getDesc(errorCode));
        publishLog.setCreateTime(new Date());
        int i = publishLogDao.save(publishLog);
        if (i > 0){
            return JsonUtil.toSuccess("保存日志成功!", "publishLog-tab", false);
        }
        return JsonUtil.toError("保存日志失败!");
    }

    /**
     * 保存日志
     * @param siteId 站点ID
     * @param categoryId 栏目ID
     * @param tplName 模板名称
     * @param message 信息
     * @return
     */
    @Override
    public String save(Integer siteId, Long categoryId, String tplName, String message) {
        PublishLog publishLog = new PublishLog();
        publishLog.setSiteId(siteId);
        publishLog.setCategoryId(Convert.toInt(categoryId));
        publishLog.setTemplateName(tplName);
        publishLog.setDescription(message);
        publishLog.setCreateTime(new Date());
        int i = publishLogDao.save(publishLog);
        if (i > 0){
            return JsonUtil.toSuccess("保存日志成功!", "publishLog-tab", false);
        }
        return JsonUtil.toError("保存日志失败!");
    }

    /**
     * 修改日志
     *
     * @param publishLog 日志实体
     * @return
     * @author 胡佳明
     * @date 2019/4/26 15:57
     */
    @Override
    public String update(PublishLog publishLog) {
        int i = publishLogDao.update(publishLog);
        if (i > 0){
            return JsonUtil.toSuccess("修改日志成功!", "publishLog-tab", false);
        }
        return JsonUtil.toError("修改日志失败!");
    }

    /**
     * 查询静态化发布日志信息(分页查询)
     *
     * @param pageNumer 当前页
     * @param pageSize  每页大小
     * @param publishLog 日志实体
     * @return java.lang.String
     * @author 胡佳明
     * @date 2019/4/26 15:57
     */
    @Override
    public PageInfo<PublishLog> page(Integer pageNumer, Integer pageSize, PublishLog publishLog) {

        PageHelper.startPage(pageNumer,pageSize);

        return new PageInfo<>(publishLogDao.selectAll(publishLog));
    }

    /**
     * 删除日志
     *
     * @param ids 日志ID
     * @return
     * @author 胡佳明
     * @date 2019/4/26 19:15
     */
    @Override
    public String delete(Integer[] ids) {
        boolean flag = false;
        if (ids.length > 0){
            for (Integer id : ids) {
                if (publishLogDao.deleteByPrimaryKey(id) > 0){
                    flag = true;
                }
            }
        }
        if (flag){
            return JsonUtil.toSuccess("删除日志成功!","publishLog-tab", false);
        }
        return JsonUtil.toError("删除日志失败!");
    }
}
